/**
 * Copyright(C) 2008 Matthias Braunhofer
 * All rights reserved
 *
 * Created on: 28.07.2008
*/

package it.cums.wizards;

import it.cums.controller.ApplicationController;
import it.cums.model.Customers;
import it.cums.util.CustomerManager;

import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWizard;

public class CustomerWizard extends Wizard implements IWorkbenchWizard {
	private CustomerBasicWizardPage customerBasicWizardPage;
	private CustomerPersonalWizardPage customerPersonalWizardPage;
	private CustomerBusinessWizardPage customerBusinessWizardPage;
	private CustomerNotesWizardPage customerNotesWizardPage;
	private ApplicationController appController = ApplicationController.getInstance();
	
	public CustomerWizard() {
		super();
		setNeedsProgressMonitor(true);
		setWindowTitle("New Customer");
	}

	@Override
	public void addPages() {
		// TODO Auto-generated method stub
		customerBasicWizardPage = new CustomerBasicWizardPage();
		addPage(customerBasicWizardPage);
		customerPersonalWizardPage = new CustomerPersonalWizardPage();
		addPage(customerPersonalWizardPage);
		customerBusinessWizardPage = new CustomerBusinessWizardPage();
		addPage(customerBusinessWizardPage);
		customerNotesWizardPage = new CustomerNotesWizardPage();
		addPage(customerNotesWizardPage);
	}
	
	@Override
	public boolean performFinish() {
		// TODO Auto-generated method stub
		Customers customers = appController.getCustomers();
		try {
			CustomerManager.getInstance().createAndStoreCustomer(customers, customerBasicWizardPage.getName(), customerBasicWizardPage.getReferencePerson(), customerBasicWizardPage.getTaxNumber(), 
					customerPersonalWizardPage.getAddress(), customerPersonalWizardPage.getCAP(), customerPersonalWizardPage.getLocation(), customerPersonalWizardPage.getCountry(), customerPersonalWizardPage.getPhone(), 
					customerPersonalWizardPage.getMobilePhone(), customerPersonalWizardPage.getEmail(), customerPersonalWizardPage.getWebSite(), customerBusinessWizardPage.getDeliveryAddress(), 
					customerBusinessWizardPage.getDeliveryCAP(), customerBusinessWizardPage.getDeliveryLocation(), customerBusinessWizardPage.getDeliveryCountry(), customerNotesWizardPage.getNotes()); 
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public void init(IWorkbench arg0, IStructuredSelection arg1) {
	}
}
